package com.velpro.im.controller;

import com.velpro.im.controller.base.Result;
import com.velpro.im.controller.msg.Greeting;
import com.velpro.im.mapper.po.Example;
import com.velpro.im.service.ExampleService;
import com.velpro.im.service.UserService;
import com.velpro.im.service.common.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author 模板例子
 *
 */
@RestController
@RequestMapping("/example")
public class ExampleController {

    private static final String template = "Hello, %s!";
    private final AtomicLong counter = new AtomicLong();

    @Autowired
    private ExampleService exampleService;

    /**
     * 获取对象基本信息
     * @param name
     * @return
     */
    @RequestMapping(value = "/get",method = RequestMethod.GET)
    @ResponseBody
    public Result get(@RequestParam(value = "id") Integer id) {
        Example data = this.exampleService.selectByPrimaryKey(id);
        return Result.ok(data);
    }

    /**
     * 增加一个对象
     * @param id
     * @param name
     * @return
     */
    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public Result insert(@RequestParam(value = "id", defaultValue = "1002") Long id,
                              @RequestParam(value = "name", defaultValue = "World") String name) {
        Example data = new Example();
        data.setName("测试");
        data.setCreateUserId(1);
        data.setCreateTime(new Date());
        data.setUpdateTime(new Date());
        data.setFlag(0);
        this.exampleService.insert(data);
        return Result.ok();
    }

    /**
     * @param
     * @return
     */
    @RequestMapping(value = "/update", method = RequestMethod.GET)
    public Result update(/*@RequestBody Greeting greeting*/) {
        Example data = this.exampleService.selectByPrimaryKey(1);
        data.setName("反反复复");
        data.setUpdateUserId(1);
        data.setUpdateTime(new Date());
        this.exampleService.updateInfo(data);
        return Result.ok();
    }

    /**
     * @param
     * @return
     */
    @RequestMapping(value = "/delete", method = RequestMethod.GET, headers = { "Accept=application/json" })
    public Result delete(@RequestParam(value = "id") Integer id) {
        this.exampleService.delete(id);
        return Result.ok();
    }

}
